package com.itheima.app.controller.umsController;

import com.itheima.app.manager.umsManager.UmsRoleManager;
import com.itheima.commons.R;
import com.itheima.domain.db.ums.UmsRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UmsRoleController {

    @Autowired
    UmsRoleManager umsRoleManager;

    /*
     * 分页查询用户信息
     * */
    @GetMapping("/role/list")
    public R findByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                        @RequestParam(required = false) String keyword) {

        return umsRoleManager.findByPage(pageNum, pageSize,keyword);

    }

    // 获取全部角色
    @GetMapping("/role/listAll")
    public R findAll() {

        return umsRoleManager.findAll();

    }

    // 添加角色
    @PostMapping("/role/create")
    public R creatRole(@RequestBody UmsRole umsRole) {
        return umsRoleManager.creatRole(umsRole);
    }

    // 禁用角色 修改角色状态
    @PostMapping("/role/updateStatus/{id}")
    public R updateRoleStatus(@PathVariable("id") Integer roleId, Integer status){
        return umsRoleManager.updateRoleStatus(roleId,status);
    }

    // 修改角色的信息
    @PostMapping("/role/update/{id}")
    public R updateRoleInfo(@PathVariable("id") Integer roleId, @RequestBody UmsRole umsRole){
        return umsRoleManager.updateRoleInfo(roleId,umsRole);
    }

    // 批量删除角色
    // 1 如果该角色下 有用户再使用 是不能删除的
    // 2 如果该角色可以被删除 需要将角色和菜单关系表中的数据也删除
    // 3 还需要将角色资源关系表删除
    @PostMapping("/role/delete")
    public R deleteRoleByIds(@RequestParam List<Long> ids){
        return umsRoleManager.deleteRoleByIds(ids);
    }

    //获取角色相关菜单
    @GetMapping("role/listMenu/{id}")
    public R findRoleMenu(@PathVariable("id")Long roleId){
        return umsRoleManager.findRoleMenu(roleId);
    }

    // 给角色分配菜单
    @PostMapping("/role/allocMenu")
    public R roleAllocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds){
        return umsRoleManager.roleAllocMenu(roleId,menuIds);
    }

    //获取角色相关资源
    @GetMapping("role/listResource/{roleId}")
    public R findRoleResourceListByRoleId(@PathVariable Long roleId){
        return umsRoleManager.findRoleResourceListByRoleId(roleId);
    }

    //给指定角色分配资源
    @PostMapping("role/allocResource")
    public R roleAllocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds){
        return umsRoleManager.roleAllocResource(roleId,resourceIds);
    }

}
